﻿using Inovout.Cloud.DatabaseCluster.Models;
using Inovout.Cloud.DatabaseCluster.Repositories;
using Inovout.Cloud.Models;
using Inovout.Cloud.Namespace.Models;
using Inovout.Cloud.Namespace.Services;
using Inovout.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Inovout.Cloud.DatabaseCluster.Service
{
    public class DatabaseService : RepositoryService<Inovout.Cloud.DatabaseCluster.Models.Database>, IDatabaseService 
    {
        protected DatabaseRepository DatabaseRepository
        {
            get { return (DatabaseRepository)base.Repository; }
        }

        public override void Save(Inovout.Cloud.DatabaseCluster.Models.Database database) 
        {
            Inovout.Cloud.DataStore.Services.DatabaseService databaseService = (Inovout.Cloud.DataStore.Services.DatabaseService)ComponentRegistry.Resolve<Inovout.Cloud.DataStore.Services.IDatabaseService>();
            //已存在，直接返回 todo: 变为前端验证
            if (!databaseService.Exists(database.Name))
            {
                return;
            }
            base.Save(database);
            Inovout.Cloud.DataStore.Models.Database dataStoreDatabase=new DataStore.Models.Database();
            dataStoreDatabase.Name=database.Name;
            dataStoreDatabase.Project = database.Project;
            dataStoreDatabase = databaseService.CreateDatabase(dataStoreDatabase);
        }
        public IList<Inovout.Cloud.DatabaseCluster.Models.Database> FindAllByProject(Project project)
        {
            return DatabaseRepository.FindByProject(project);
        }
    }
}
